package xyz.lisys.boot.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import xyz.lisys.boot.filter.MyCustomFilter;

import javax.servlet.DispatcherType;

public class WebMvcConfigurer {

    @Bean
    public FilterRegistrationBean<MyCustomFilter> myCustomFilterRegistration() {
        // 用于在Spring Boot应用中注册和配置Filter
        FilterRegistrationBean<MyCustomFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyCustomFilter());
        registrationBean.addUrlPatterns("/*"); // 设置URL匹配模式
        registrationBean.setName("myCustomFilter"); // 设置Filter名称
        registrationBean.setOrder(1); // 设置执行顺序，值越小，执行顺序越靠前
        registrationBean.setAsyncSupported(true); // 设置是否支持异步处理
        registrationBean.addInitParameter("paramName", "paramValue"); // 设置初始化参数

        // 设置DispatcherType，指定Filter应该应用到哪些类型的请求
        // 如：REQUEST、FORWARD、INCLUDE、ERROR、ASYNC
        registrationBean.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD);

        return registrationBean;
    }
}
